package com.teach.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.teach.R;
import com.teach.commons.SecurityCodeTool;
import com.teach.dto.LoginUser;
import com.teach.dto.UserRegFormDTO;
import com.teach.dto.UserSearch;
import com.teach.entity.UserInfo;
import com.teach.exception.CodeException;
import com.teach.service.FileService;
import com.teach.service.SmsService;
import com.teach.service.UserService;

import lombok.extern.slf4j.Slf4j;

@RequestMapping("/user")
//@Controller
//@RestController = @Controller + @ResponseBody
@RestController
@Slf4j
public class UserController {
	
	@Autowired
	private UserService service;
	
	@Autowired
	private SmsService smsService;
	
	@Autowired
	private FileService fileService;

	@RequestMapping("login.do")
	public R doLogin(String userName,String userPwd,String code,HttpSession session,HttpServletRequest request){
			SecurityCodeTool.check(session, code);
			LoginUser user =service.login(userName, userPwd);
			session.setAttribute("user", user);
			return R.ok();
	}
	
	
	@PostMapping("reg.do")
	public R doReg(UserRegFormDTO form,String code) {
		if(!smsService.check(form.getUserTel(), code)) {
			throw new CodeException(16);
		}
		service.reg(form);
		return R.ok();
	}
	
	@PostMapping("tel-code.do")
	public R sendTelCode(String userTel) {
		if(service.existsUserTel(userTel)) {
			throw new CodeException(7);
		}
		smsService.send(userTel);
		return R.ok();
	}
	
	@PostMapping("exists-name.do")
	public R doExistsName(String userName) {
		return R.empty().put("valid", !service.existsUserName(userName));
	}
	
	@PostMapping("exists-tel.do")
	public R doExistsTel(String userTel) {
		return R.empty().put("valid", !service.existsUserTel(userTel));
	}

	
	@RequestMapping("info.do")
	public Object doUser(LoginUser user){
		return service.get(user.getUserName());
	}
	
	@RequestMapping("login-user.do")
	public Object doLoginUser(LoginUser user){
		return user;
	}
	
	@RequestMapping("/logout.do")
	public R doLogout(HttpSession session){
		session.invalidate();
		return R.ok();
	}
	
	@RequestMapping("/list.do")
	public Object doList(UserSearch search) {
		return service.list(search);
	}
	
	@RequestMapping("/page.do")
	public Object doPage(UserSearch search) {
		return service.page(search);
	}
	
	@PostMapping("/modify.do")
	public R doModify(UserInfo userInfo) {
		service.modify(userInfo);
		return R.ok();
	}
	
	@PostMapping("/modify-pwd.do")
	public R doModifyPwd(String userName,String userPwd) {
		service.modify(userName, userPwd);
		return R.ok();
	}
	
	@PostMapping("/my-pwd.do")
	public R doMyPwd(LoginUser user,String newUserPwd,String oldUserPwd) {
		service.modify(user.getUserName(), newUserPwd, oldUserPwd);
		return R.ok();
	}
	
	@PostMapping("/my-img.do")
	public R doMyImg(LoginUser user,MultipartFile file) {
		String url = fileService.save(file, true);
		log.debug("{}", url);
		UserInfo u = new UserInfo();
		u.setUserName(user.getUserName());
		u.setUserImg(url);
		service.modify(u);
		return R.ok();
	}
	
	@PostMapping("/my-info.do")
	public R doMyImg(LoginUser user,UserInfo u) {
		u.setUserName(user.getUserName());
		
		//设置不允许修改的字段
		u.setUserImg(null);
		u.setRoleId(null);
		u.setUserLocked(null);
		u.setUserPwd(null);
		u.setUserTel(null);
		service.modify(u);
		return R.ok();
	}
	@RequestMapping("/get-realName.do")
	@ResponseBody
	public Object getRealName(String userName) {
		return service.get(userName);
	}
}
